const express = require("express");
const query = require("../sql/mysql");
const Router = express.Router();
//渲染货物到页面
Router.get("/goods/all", async(req, res) => {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,PATCH,POST,GET,DELETE,OPTIONS");
    let page = req.query.page;
    let type = req.query.type;
    page = page || 1;
    type = type || 0;
    let num = 20;
    let index = (page - 1) * num;
    let data;
    if (type == 0) {
        let sql = `SELECT * FROM goods LIMIT ${index},${num}`;
        data = await query(sql);
    } else if (type == 1) {
        let sql = `SELECT * FROM goods ORDER BY price DESC LIMIT ${index},${num}`;
        data = await query(sql);
    } else {
        let sql = `SELECT * FROM goods ORDER BY price ASC LIMIT ${index},${num}`;
        data = await query(sql);
    }
    let sql1 = `SELECT * FROM goods`;
    let data1 = await query(sql1);
    let obj = {}
    if (data.length) {
        let totalPage = Math.ceil(data1.length / num);
        obj = {
            msg: "成功",
            page,
            totalPage,
            list: data
        };
    } else {
        obj = {
            msg: "失败",
        };
    }
    res.send(obj)
});
//购物车
Router.post("/goods/cart", express.urlencoded(), express.json(), async(req, res) => {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,PATCH,POST,GET,DELETE,OPTIONS");
    let type = req.body.type; //关键字，必填，接口功能由这个参数决定
    let id = req.body.id;
    let num = req.body.num;
    let flag = req.body.flag;
    let good_id = req.body.good_id;
    let obj = {};
    if (type == "add") {
        //关键字为add的时候，该接口的功能为该用户购物车数量+1
        //需要传参: good_id 、id、num
        let sql = `SELECT * FROM cart WHERE good_id = ${good_id} AND id=${id}`;
        let data = await query(sql);
        if (data.length == 0) {
            let sql1 = `INSERT INTO cart (cart_id,id, good_id, num) VALUES (NULL, ${id},${good_id},${num})`;
            let data1 = await query(sql1);
            if (data1.affectedRows != 0) {
                obj.msg = "商品添加成功"
            } else {
                obj.msg = "商品添加失败"
            }
        } else {
            let sql1 = `UPDATE cart SET num= num+ ${num} WHERE good_id=${good_id} AND id=${id}`;
            let data1 = await query(sql1);
            if (data1.affectedRows != 0) {
                obj.msg = "数量更新成功"
            } else {
                obj.msg = "数量更新失败"
            }
        }
        res.send(obj);
    } else if (type == "get") {
        //关键字为get的时候，该功能为获取该用户所有的购物车信息
        //需要传参:id
        let sql = `SELECT cart.*,goods.name,goods.src,goods.price FROM cart , goods WHERE cart.good_id = goods.good_id AND id=${id}`;
        let data = await query(sql);
        obj.msg = "获取成功";
        obj.data = data;
        res.send(obj);
    } else if (type == "update") {
        //关键字为update的时候，该功能为加1或减1购物车指定商品
        //需要传参:id、good_id
        let data;
        if (flag == "plus") {
            let sql = `UPDATE cart SET num= num+ 1 WHERE good_id=${good_id} AND id=${id}`;
            data = await query(sql);
        } else if (flag == "reduce") {
            let sql = `UPDATE cart SET num= num- 1 WHERE good_id=${good_id} AND id=${id}`;
            data = await query(sql);
        }
        if (data.affectedRows != 0) {
            obj.msg = "修改成功"
        } else {
            obj.msg = "修改失败"
        }
        res.send(obj)
    } else if (type == "del") {
        //关键字为del的时候，该功能为删除指定商品
        //需要传参:id、good_id
        let sql = `DELETE FROM cart WHERE good_id = ${good_id} AND id=${id}`;
        let data = await query(sql);
        if (data.affectedRows != 0) {
            obj.msg = "删除成功"
        } else {
            obj.msg = "删除失败"
        }
        res.send(obj);
    }
});

module.exports = Router;